﻿using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using Ex05.MemoryGameLogic;

namespace Ex05.MemoryGameGraphicalUI
{
    public class MemoryGameSettingsForm : Form
    {
        private const string k_ComputerName = "-computer-";
        private const string k_AgainstComputerButtonText = "Against Computer";
        private const string k_AgainstHumanButtonText = "Against a Friend";

        private Game.eOpponentType m_CurrentOpponentType;
        private Label m_FirstPlayerNameLabel;
        private Label m_SecondPlayerNameLabel;
        private Label m_BoardSizeLabel;
        private TextBox m_FirstPlayerNameTextBox;
        private TextBox m_SecondPlayerNameTextBox;
        private Button m_ChangeOpponentButton;
        private Button m_ChangeBoardSizeButton;
        private Button m_StartButton;
        private List<BoardSize> m_AllowedBoardSized;
        private int m_CurrentBoardSizeIndex;
        private string m_LastSecondPlayerName;
        private Game m_Game;

        public MemoryGameSettingsForm()
        {
            initializeComponents();
            initializeUILogic();
        }

        /// <summary>
        /// Returns the game the settings form initializes.
        /// </summary>
        public Game Game
        {
            get { return m_Game; }
        }

        private void initializeComponents()
        {
            this.Text = "Memory Game - Settings";
            this.Size = new Size(412, 190);
            this.MinimizeBox = false;
            this.MaximizeBox = false;
            this.FormClosing += new FormClosingEventHandler(MemoryGameSettingsForm_FormClosing);

            m_FirstPlayerNameLabel = new Label();
            m_SecondPlayerNameLabel = new Label();
            m_BoardSizeLabel = new Label();
            m_FirstPlayerNameLabel.AutoSize = true;
            m_SecondPlayerNameLabel.AutoSize = true;
            m_FirstPlayerNameLabel.TabIndex = 1;
            m_SecondPlayerNameLabel.TabIndex = 3;
            m_BoardSizeLabel.TabIndex = 6;
            m_BoardSizeLabel.AutoSize = true;
            m_FirstPlayerNameLabel.Anchor = AnchorStyles.Left | AnchorStyles.Top;
            m_SecondPlayerNameLabel.Anchor = AnchorStyles.Left | AnchorStyles.Top;
            m_BoardSizeLabel.Anchor = AnchorStyles.Left | AnchorStyles.Top;
            m_FirstPlayerNameLabel.Location = new Point(10, 10);
            m_SecondPlayerNameLabel.Location = new Point(m_FirstPlayerNameLabel.Left, m_FirstPlayerNameLabel.Bottom);
            m_BoardSizeLabel.Location = new Point(m_SecondPlayerNameLabel.Left, m_SecondPlayerNameLabel.Bottom);
            m_FirstPlayerNameLabel.Text = "First Player Name:";
            m_SecondPlayerNameLabel.Text = "Second Player Name:";
            m_BoardSizeLabel.Text = "Board Size:";

            this.Controls.Add(m_FirstPlayerNameLabel);
            this.Controls.Add(m_SecondPlayerNameLabel);
            this.Controls.Add(m_BoardSizeLabel);

            m_FirstPlayerNameTextBox = new TextBox();
            m_SecondPlayerNameTextBox = new TextBox();
            m_FirstPlayerNameTextBox.TabIndex = 2;
            m_SecondPlayerNameTextBox.TabIndex = 4;
            int textBoxesLeftCoordinate = m_FirstPlayerNameLabel.Right > m_SecondPlayerNameLabel.Right ? m_FirstPlayerNameLabel.Right : m_SecondPlayerNameLabel.Right;
            textBoxesLeftCoordinate += 10;
            int firstTextBoxTop = m_FirstPlayerNameLabel.Top + (m_FirstPlayerNameLabel.Height / 2)
                - (m_FirstPlayerNameTextBox.Height / 2);
            int secondTextBoxTop = m_SecondPlayerNameLabel.Top + (m_SecondPlayerNameLabel.Height / 2)
                - (m_SecondPlayerNameTextBox.Height / 2);

            m_FirstPlayerNameTextBox.Location = new Point(textBoxesLeftCoordinate, firstTextBoxTop);
            m_SecondPlayerNameTextBox.Location = new Point(textBoxesLeftCoordinate, secondTextBoxTop);
            m_FirstPlayerNameTextBox.Width = 120;
            m_SecondPlayerNameTextBox.Width = m_FirstPlayerNameTextBox.Width;
            m_FirstPlayerNameTextBox.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
            m_SecondPlayerNameTextBox.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;

            this.Controls.Add(m_FirstPlayerNameTextBox);
            this.Controls.Add(m_SecondPlayerNameTextBox);

            m_ChangeOpponentButton = new Button();
            m_ChangeOpponentButton.TabIndex = 5;
            m_ChangeOpponentButton.Width = 120;
            m_ChangeOpponentButton.Height = 23;
            int changeOpponentButtonTop = m_SecondPlayerNameTextBox.Top + (m_SecondPlayerNameTextBox.Height / 2)
                - (m_ChangeOpponentButton.Height / 2) - 1;
            m_ChangeOpponentButton.Top = changeOpponentButtonTop;
            m_ChangeOpponentButton.Left = m_SecondPlayerNameTextBox.Right + 10;
            m_ChangeOpponentButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            m_ChangeOpponentButton.Click += new EventHandler(m_ChangeOpponentButton_Click);

            this.Controls.Add(m_ChangeOpponentButton);

            m_ChangeBoardSizeButton = new Button();
            m_ChangeBoardSizeButton.TabIndex = 7;
            m_ChangeBoardSizeButton.Left = m_BoardSizeLabel.Left;
            m_ChangeBoardSizeButton.Top = m_BoardSizeLabel.Bottom + 5;
            m_ChangeBoardSizeButton.Width = 90;
            m_ChangeBoardSizeButton.Height = 65;
            m_ChangeBoardSizeButton.BackColor = Color.CornflowerBlue;
            m_ChangeBoardSizeButton.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom;
            m_ChangeBoardSizeButton.Click += new EventHandler(m_ChangeBoardSizeButton_Click);

            this.Controls.Add(m_ChangeBoardSizeButton);

            m_StartButton = new Button();
            m_StartButton.TabIndex = 8;
            m_StartButton.BackColor = Color.Green;
            m_StartButton.Width = 75;
            m_StartButton.Height = 23;
            m_StartButton.Location = new Point(
                m_ChangeOpponentButton.Right - m_StartButton.Width,
                m_ChangeBoardSizeButton.Bottom - m_StartButton.Height);
            m_StartButton.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            m_StartButton.Text = "Start!";
            m_StartButton.Click += new EventHandler(m_StartButton_Click);
            this.AcceptButton = m_StartButton;

            this.Controls.Add(m_StartButton);
        }

        private void MemoryGameSettingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_Game.FirstPlayerName = m_FirstPlayerNameTextBox.Text;
            m_Game.SecondPlayerName = m_SecondPlayerNameTextBox.Text;
            m_Game.OpponentType = m_CurrentOpponentType;
            m_Game.BoardSize = m_AllowedBoardSized[m_CurrentBoardSizeIndex];
        }

        private void m_StartButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void m_ChangeBoardSizeButton_Click(object sender, EventArgs e)
        {
            setNextBoardSize();
        }

        private void m_ChangeOpponentButton_Click(object sender, EventArgs e)
        {
            switchOpponentType();
        }

        private void initializeUILogic()
        {
            m_Game = new Game();
            m_LastSecondPlayerName = string.Empty;
            setOpponentType(Game.eOpponentType.Computer);

            m_AllowedBoardSized = m_Game.GetAllowedBoardSizes();
            m_CurrentBoardSizeIndex = m_AllowedBoardSized.Count - 1;
            setNextBoardSize();
        }

        private void setNextBoardSize()
        {
            m_CurrentBoardSizeIndex++;

            if (m_CurrentBoardSizeIndex > m_AllowedBoardSized.Count - 1)
            {
                m_CurrentBoardSizeIndex -= m_AllowedBoardSized.Count;
            }

            m_ChangeBoardSizeButton.Text = m_AllowedBoardSized[m_CurrentBoardSizeIndex].ToString();
        }

        private void switchOpponentType()
        {
            Game.eOpponentType newOpponentType =
                m_CurrentOpponentType == Game.eOpponentType.Computer ? Game.eOpponentType.Human : Game.eOpponentType.Computer;
            setOpponentType(newOpponentType);
        }

        private void setOpponentType(Game.eOpponentType i_OpponentType)
        {
            m_CurrentOpponentType = i_OpponentType;

            switch (m_CurrentOpponentType)
            {
                case Game.eOpponentType.Computer:
                    m_ChangeOpponentButton.Text = k_AgainstHumanButtonText;
                    m_LastSecondPlayerName = m_SecondPlayerNameTextBox.Text;
                    m_SecondPlayerNameTextBox.Text = k_ComputerName;
                    m_SecondPlayerNameTextBox.Enabled = false;
                    break;
                case Game.eOpponentType.Human:
                    m_ChangeOpponentButton.Text = k_AgainstComputerButtonText;
                    m_SecondPlayerNameTextBox.Text = m_LastSecondPlayerName;
                    m_SecondPlayerNameTextBox.Enabled = true;
                    break;
            }
        }
    }
}